package com.bonc.rms.utils.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bonc.odk.logmanager.LogManager;
//import com.bonc.rms.entity.tms.Salary;
//import com.bonc.rms.entity.tms.SalaryRecord;
import com.bonc.rms.entity.tms.Talent;
//import com.bonc.rms.entity.tms.TalentEntity;
import com.bonc.rms.mapper.EmployeeRRUtilMapper;

@Component
public class EmployeeRRUtilImpl {

	@Autowired
	EmployeeRRUtilMapper employeeRRUtilMapper;
	@Autowired
	WastageInfoUtilImpl wastageInfoUtilImpl;

	/**
	 * 
	 * @param year
	 * @param departmentId
	 * @return 这个年份的现役员工
	 */
	public List<String> getActiveDutyYear(String year,
			Integer departmentStandardId) {
		LogManager
				.Method("Int Service : EmployeeRRUtilImpl -> getActiveDutyYear");
		List<String> employeeList = new ArrayList<>();

		List<Integer> departmentIdlist = new ArrayList<>();
		departmentIdlist = wastageInfoUtilImpl
				.getDepartmentId(departmentStandardId);
		if ((departmentIdlist == null || departmentIdlist.size() == 0)
				&& departmentStandardId > 2) {

		} else {
			employeeList = employeeRRUtilMapper.getYearEmployee(year,
					departmentIdlist, departmentIdlist.size());

		}
		LogManager
				.Method("Out Service : EmployeeRRUtilImpl -> getActiveDutyYear");
		return employeeList;

	}

	/**
	 * 
	 * @param employeeList
	 * @return 这个年份的每个现役员工总工资的计算信息 入职时间 离职时间 调薪日期(list) 距离本年度最近的调薪日期
	 */
	public Map<String, Object> getEmployeeInfo(String jobNumber, String year) {
		LogManager
				.Method("Int Service : EmployeeRRUtilImpl -> getEmployeeInfo");

		Map<String, Object> employeeInfoMap = new HashMap<>();

		/**
		 * 解决工号重复时报错的问题
		 */
		List<Talent> talentList = employeeRRUtilMapper
				.getEmployeeInfo(jobNumber);
		Talent talent = null;
		if (talentList.size() > 0) {
			talent = talentList.get(0);
		}

		employeeInfoMap.put("离职时间", talent.getLeave_date());
		employeeInfoMap.put("入职时间", talent.getEntry_date());

		List<String> adjustDateList = new ArrayList<>();
		adjustDateList = employeeRRUtilMapper.getAdjustDate(jobNumber, year);
		employeeInfoMap.put("调薪日期", adjustDateList);

		String endAdjustDate = new String();
		endAdjustDate = employeeRRUtilMapper.getEndAdjustDate(jobNumber, year);
		employeeInfoMap.put("距离本年度最近的调薪日期", endAdjustDate);

		employeeInfoMap.put("员工工号", jobNumber);

		LogManager
				.Method("Out Service : EmployeeRRUtilImpl -> getEmployeeInfo");
		return employeeInfoMap;

	}

	/**
	 * 获取总工资方法
	 */
	@SuppressWarnings("unchecked")
	public String getTotalWages(List<String> timeList, String year) {
		LogManager.Method("Int Service : EmployeeRRUtilImpl -> getTotalWages");
		int sumSalary = 0;

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for (String jobNumber : timeList) {
			Map<String, Object> employeeMap = new HashMap<>();
			employeeMap = getEmployeeInfo(jobNumber, year);
			int everyoneSumSalary = 0;// 个人年度总工资

			String jobNumberStr = (String) employeeMap.get("员工工号");

			Date leave = (Date) employeeMap.get("离职时间");// 本年度离职的信息
			String leaveStr = null;
			String leaveYearStr = null;
			String leaveMonthStr;
			int leaveMonthInt = 0;
			if (leave != null) {
				leaveStr = sdf.format(leave);
				leaveYearStr = leaveStr.substring(0, 4);
				leaveMonthStr = (leaveStr.substring(5, 6)).equals("0") ? leaveStr
						.substring(6, 7) : leaveStr.substring(5, 7);
				leaveMonthInt = Integer.parseInt(leaveMonthStr);
			}

			Date entry = (Date) employeeMap.get("入职时间");

			String entryStr = sdf.format(entry);
			String entryYearStr = entryStr.substring(0, 4);
			String entryMonthStr = (entryStr.substring(5, 6)).equals("0") ? entryStr
					.substring(6, 7) : entryStr.substring(5, 7);
			int entryMonthInt = Integer.parseInt(entryMonthStr);

			String finallyAdjust = (String) employeeMap.get("距离本年度最近的调薪日期");

			List<String> adjustList = new ArrayList<>();
			adjustList = (List<String>) employeeMap.get("调薪日期");
			// 本年度有离职
			Date date = new Date();
			String thisYear = sdf.format(date);
			String thisYearStr = thisYear.substring(0, 4);
			String thisYeatMonthStr = (thisYear.substring(5, 6)).equals("0") ? thisYear
					.substring(6, 7) : thisYear.substring(5, 7);
			int thisYearMonthInt = Integer.parseInt(thisYeatMonthStr);
			// 判断年份是否是今年
			if (year.equals(thisYearStr)) {
				if (year.equals(leaveYearStr)) {

					// ***************************本年度有离职且无调薪
					if (adjustList == null || adjustList.size() == 0) {
						// 本年度之前不存在过调薪
						if (finallyAdjust == null || finallyAdjust.equals("")) {
							int trialSalary = 0;
							trialSalary = getTrialMonthSalary(jobNumberStr);// 试用期每个月总工资

							if (year.equals(entryYearStr)) {// 本年度入得职

								everyoneSumSalary = (leaveMonthInt - entryMonthInt)
										* trialSalary;
							} else {// 本年度之前入得职

								everyoneSumSalary = (leaveMonthInt - 1)
										* trialSalary;
							}
						} else {
							// 本年度之前存在过调薪
							int adjustSalary = 0;
							adjustSalary = getAdjustMonthSalary(jobNumberStr,
									finallyAdjust);// 调薪后每个月总工资
							everyoneSumSalary = (leaveMonthInt - 1)
									* adjustSalary;
						}

					} else {
						// ***************************本年度有离职且有调薪
						for (String adjustStr : adjustList) {// adjustList中日期从12月份——>1月份顺序排列
							int i = 0;
							int lastAdjustMonthInt = 0;

							// String adjustYear = adjustStr.substring(0, 4);
							String adjustMonth = (adjustStr.substring(5, 6))
									.equals("0") ? adjustStr.substring(6, 7)
									: adjustStr.substring(5, 7);
							int adjustMonthInt = Integer.parseInt(adjustMonth);

							// 还没转正就以离职
							// 调薪日期大于离职日期只有一种可能就是实习生离职
							if (adjustMonthInt > leaveMonthInt) {
								int trialSalary = 0;
								trialSalary = getTrialMonthSalary(jobNumberStr);

								if (year.equals(entryYearStr)) {// 本年度入得职

									everyoneSumSalary = (leaveMonthInt - entryMonthInt)
											* trialSalary;
								} else {// 本年度之前入得职

									everyoneSumSalary = (leaveMonthInt - 1)
											* trialSalary;
								}
							} else {// 正常调薪
								int adjustSalary = 0;
								adjustSalary = getAdjustMonthSalary(
										jobNumberStr, finallyAdjust);
								if (i == 0) {// 离职到第一次调薪总工资
									everyoneSumSalary = (leaveMonthInt - adjustMonthInt)
											* adjustSalary;

								} else {// 后面调薪之间总工资
									everyoneSumSalary += (lastAdjustMonthInt - adjustMonthInt)
											* adjustSalary;
								}
								if (i == adjustList.size()) {// 本年第一次调薪之前的总工资

									if (finallyAdjust == null
											|| finallyAdjust.equals("")) {// 本年度之前不存在过调薪

										int trialSalary = 0;
										trialSalary = getTrialMonthSalary(jobNumberStr);

										if (year.equals(entryYearStr)) {// 本年度入得职

											everyoneSumSalary += (lastAdjustMonthInt - entryMonthInt)
													* trialSalary;
										} else {// 本年度之前入得职

											everyoneSumSalary += (lastAdjustMonthInt - 1)
													* trialSalary;
										}
									} else {
										// 本年度之前存在过调薪
										int finallyAdjustSalary = 0;
										finallyAdjustSalary = getAdjustMonthSalary(
												jobNumberStr, finallyAdjust);
										everyoneSumSalary += (lastAdjustMonthInt - 1)
												* finallyAdjustSalary;
									}

								}
							}
							i += 1;
							lastAdjustMonthInt = adjustMonthInt;
						}

					}
				} else {

					// ***************************本年度无离职且无调薪
					if (adjustList == null || adjustList.size() == 0) {
						// 本年度之前不存在过调薪
						if (finallyAdjust == null || finallyAdjust.equals("")) {
							int trialSalary = 0;
							trialSalary = getTrialMonthSalary(jobNumberStr);

							if (year.equals(entryYearStr)) {// 本年度入得职

								everyoneSumSalary = (thisYearMonthInt - entryMonthInt)
										* trialSalary;
							} else {// 本年度之前入得职

								everyoneSumSalary = thisYearMonthInt
										* trialSalary;
							}
						} else {
							// 本年度之前存在过调薪
							int adjustSalary = 0;
							adjustSalary = getAdjustMonthSalary(jobNumberStr,
									finallyAdjust);
							everyoneSumSalary = thisYearMonthInt * adjustSalary;
						}

					} else {
						// ***************************本年度无离职但有调薪
						for (String adjustStr : adjustList) {// adjustList中日期从12月份——>1月份顺序排列
							int i = 0;
							int lastAdjustMonthInt = 0;

							// String adjustYear = adjustStr.substring(0, 4);
							String adjustMonth = (adjustStr.substring(5, 6))
									.equals("0") ? adjustStr.substring(6, 7)
									: adjustStr.substring(5, 7);
							int adjustMonthInt = Integer.parseInt(adjustMonth);

							// 还没转正
							// 调薪日期大于离职日期只有一种可能就是实习生离职
							if (adjustMonthInt > thisYearMonthInt) {
								int trialSalary = 0;
								trialSalary = getTrialMonthSalary(jobNumberStr);

								if (year.equals(entryYearStr)) {// 本年度入得职

									everyoneSumSalary = (thisYearMonthInt - entryMonthInt)
											* trialSalary;
								} else {// 本年度之前入得职

									everyoneSumSalary = (thisYearMonthInt - 1)
											* trialSalary;
								}
							} else {
								// 正常调薪
								int adjustSalary = 0;
								adjustSalary = getAdjustMonthSalary(
										jobNumberStr, finallyAdjust);
								if (i == 0) {// 离职到第一次调薪总工资
									everyoneSumSalary = (thisYearMonthInt - adjustMonthInt)
											* adjustSalary;

								} else {// 后面调薪之间总工资
									everyoneSumSalary += (lastAdjustMonthInt - adjustMonthInt)
											* adjustSalary;
								}
								if (i == adjustList.size()) {// 本年第一次调薪之前的总工资
									if (finallyAdjust == null
											|| finallyAdjust.equals("")) {
										int trialSalary = 0;
										trialSalary = getTrialMonthSalary(jobNumberStr);

										if (year.equals(entryYearStr)) {// 本年度入得职

											everyoneSumSalary = (lastAdjustMonthInt - entryMonthInt)
													* trialSalary;
										} else {// 本年度之前入得职

											everyoneSumSalary = (lastAdjustMonthInt - 1)
													* trialSalary;
										}

									} else {
										// 本年度之前存在过调薪
										int finallyAdjustSalary = 0;
										finallyAdjustSalary = getAdjustMonthSalary(
												jobNumberStr, finallyAdjust);
										everyoneSumSalary += (lastAdjustMonthInt - 1)
												* finallyAdjustSalary;
									}

								}

								i += 1;
								lastAdjustMonthInt = adjustMonthInt;
							}

						}
					}

				}
			} else {// 年份不是今年
				if (year.equals(leaveYearStr)) {

					// ***************************本年度有离职且无调薪
					if (adjustList == null || adjustList.size() == 0) {
						// 本年度之前不存在过调薪
						if (finallyAdjust == null || finallyAdjust.equals("")) {
							int trialSalary = 0;
							trialSalary = getTrialMonthSalary(jobNumberStr);// 试用期每个月总工资

							if (year.equals(entryYearStr)) {// 本年度入得职

								everyoneSumSalary = (leaveMonthInt - entryMonthInt)
										* trialSalary;
							} else {// 本年度之前入得职

								everyoneSumSalary = (leaveMonthInt - 1)
										* trialSalary;
							}
						} else {
							// 本年度之前存在过调薪
							int adjustSalary = 0;
							adjustSalary = getAdjustMonthSalary(jobNumberStr,
									finallyAdjust);// 调薪后每个月总工资
							everyoneSumSalary = (leaveMonthInt - 1)
									* adjustSalary;
						}

					} else {
						// ***************************本年度有离职且有调薪
						for (String adjustStr : adjustList) {// adjustList中日期从12月份——>1月份顺序排列
							int i = 0;
							int lastAdjustMonthInt = 0;

							// String adjustYear = adjustStr.substring(0, 4);
							String adjustMonth = (adjustStr.substring(5, 6))
									.equals("0") ? adjustStr.substring(6, 7)
									: adjustStr.substring(5, 7);
							int adjustMonthInt = Integer.parseInt(adjustMonth);

							// 还没转正就以离职
							// 调薪日期大于离职日期只有一种可能就是实习生离职
							if (adjustMonthInt > leaveMonthInt) {
								int trialSalary = 0;
								trialSalary = getTrialMonthSalary(jobNumberStr);

								if (year.equals(entryYearStr)) {// 本年度入得职

									everyoneSumSalary = (leaveMonthInt - entryMonthInt)
											* trialSalary;
								} else {// 本年度之前入得职

									everyoneSumSalary = (leaveMonthInt - 1)
											* trialSalary;
								}
							} else {// 正常调薪
								int adjustSalary = 0;
								adjustSalary = getAdjustMonthSalary(
										jobNumberStr, finallyAdjust);
								if (i == 0) {// 离职到第一次调薪总工资
									everyoneSumSalary = (leaveMonthInt - adjustMonthInt)
											* adjustSalary;

								} else {// 后面调薪之间总工资
									everyoneSumSalary += (lastAdjustMonthInt - adjustMonthInt)
											* adjustSalary;
								}
								if (i == adjustList.size()) {// 本年第一次调薪之前的总工资

									if (finallyAdjust == null
											|| finallyAdjust.equals("")) {// 本年度之前不存在过调薪

										int trialSalary = 0;
										trialSalary = getTrialMonthSalary(jobNumberStr);

										if (year.equals(entryYearStr)) {// 本年度入得职

											everyoneSumSalary += (lastAdjustMonthInt - entryMonthInt)
													* trialSalary;
										} else {// 本年度之前入得职

											everyoneSumSalary += (lastAdjustMonthInt - 1)
													* trialSalary;
										}
									} else {
										// 本年度之前存在过调薪
										int finallyAdjustSalary = 0;
										finallyAdjustSalary = getAdjustMonthSalary(
												jobNumberStr, finallyAdjust);
										everyoneSumSalary += (lastAdjustMonthInt - 1)
												* finallyAdjustSalary;
									}

								}
							}
							i += 1;
							lastAdjustMonthInt = adjustMonthInt;
						}

					}
				} else {

					// ***************************本年度无离职且无调薪
					if (adjustList == null || adjustList.size() == 0) {
						// 本年度之前不存在过调薪
						if (finallyAdjust == null || finallyAdjust.equals("")) {
							int trialSalary = 0;
							trialSalary = getTrialMonthSalary(jobNumberStr);

							if (year.equals(entryYearStr)) {// 本年度入得职

								everyoneSumSalary = (12 - entryMonthInt)
										* trialSalary;
							} else {// 本年度之前入得职

								everyoneSumSalary = 12 * trialSalary;
							}
						} else {
							// 本年度之前存在过调薪
							int adjustSalary = 0;
							adjustSalary = getAdjustMonthSalary(jobNumberStr,
									finallyAdjust);
							everyoneSumSalary = 12 * adjustSalary;
						}

					} else {
						// ***************************本年度无离职但有调薪
						for (String adjustStr : adjustList) {// adjustList中日期从12月份——>1月份顺序排列
							int i = 0;
							int lastAdjustMonthInt = 0;

							// String adjustYear = adjustStr.substring(0, 4);
							String adjustMonth = (adjustStr.substring(5, 6))
									.equals("0") ? adjustStr.substring(6, 7)
									: adjustStr.substring(5, 7);
							int adjustMonthInt = Integer.parseInt(adjustMonth);

							// 正常调薪
							int adjustSalary = 0;
							adjustSalary = getAdjustMonthSalary(jobNumberStr,
									finallyAdjust);
							if (i == 0) {// 离职到第一次调薪总工资
								everyoneSumSalary = (12 - adjustMonthInt)
										* adjustSalary;

							} else {// 后面调薪之间总工资
								everyoneSumSalary += (lastAdjustMonthInt - adjustMonthInt)
										* adjustSalary;
							}
							if (i == adjustList.size()) {// 本年第一次调薪之前的总工资
								if (finallyAdjust == null
										|| finallyAdjust.equals("")) {
									int trialSalary = 0;
									trialSalary = getTrialMonthSalary(jobNumberStr);

									if (year.equals(entryYearStr)) {// 本年度入得职

										everyoneSumSalary = (lastAdjustMonthInt - entryMonthInt)
												* trialSalary;
									} else {// 本年度之前入得职

										everyoneSumSalary = (lastAdjustMonthInt - 1)
												* trialSalary;
									}

								} else {
									// 本年度之前存在过调薪
									int finallyAdjustSalary = 0;
									finallyAdjustSalary = getAdjustMonthSalary(
											jobNumberStr, finallyAdjust);
									everyoneSumSalary += (lastAdjustMonthInt - 1)
											* finallyAdjustSalary;
								}

							}

							i += 1;
							lastAdjustMonthInt = adjustMonthInt;
						}
					}

				}
			}

			sumSalary += everyoneSumSalary;
		}
		LogManager.Method("Out Service : EmployeeRRUtilImpl -> getTotalWages");
		return Integer.toString(sumSalary);

	}

	/**
	 * 根据员工号，调薪日期,获取这个月度调薪后的工资，绩效，奖金 总和
	 */
	public int getAdjustMonthSalary(String jobNumberStr, String adjustStr) {
		LogManager
				.Method("Int Service : EmployeeRRUtilImpl -> getAdjustMonthSalary");
		int basic = 0;
		int performance = 0;
		int bonus = 0;
		int levelT = 0;
		int sum = 0;

		List<BigDecimal> salaryRecordList = new ArrayList<>();
		salaryRecordList = employeeRRUtilMapper
				.getTrialMonthSalary(jobNumberStr);
		if (salaryRecordList.size() != 0 || salaryRecordList != null) {
			int i = 1;
			for (BigDecimal integer : salaryRecordList) {
				switch (i) {
				case 1:
					basic = integer.intValue();
					break;
				case 2:
					performance = integer.intValue();
					break;
				case 3:
					bonus = integer.intValue();
					break;
				case 4:
					levelT = integer.intValue();

					break;
				default:
					break;
				}
				i++;
			}
		}

		sum = basic + performance + bonus + levelT;
		LogManager
				.Method("Out Service : EmployeeRRUtilImpl -> getAdjustMonthSalary");
		return sum;

	}

	/**
	 * 根据员工,获取实习期每个月工资，绩效，奖金 总和
	 */
	public int getTrialMonthSalary(String jobNumberStr) {
		// 日志打印太多了，先注释掉
		// LogManager.Method("Int Service : EmployeeRRUtilImpl -> getTrialMonthSalary");
		int basic = 0;
		int performance = 0;
		int bonus = 0;
		int levelT = 0;
		int sum = 0;

		List<BigDecimal> salaryList = new ArrayList<>();
		salaryList = employeeRRUtilMapper.getTrialMonthSalary(jobNumberStr);
		if (salaryList.size() != 0 || salaryList != null) {
			int i = 1;
			for (BigDecimal integer : salaryList) {
				switch (i) {
				case 1:
					basic = integer.intValue();
					break;
				case 2:
					performance = integer.intValue();
					break;
				case 3:
					bonus = integer.intValue();
					break;
				case 4:
					levelT = integer.intValue();

					break;
				default:
					break;
				}
				i++;
			}
		}

		sum = basic + performance + bonus + levelT;
		// LogManager.Method("Out Service : EmployeeRRUtilImpl -> getTrialMonthSalary");
		return sum;

	}

	/**
	 * 
	 * @param salaryList
	 * @return 通过每年总工资求增长率
	 */
	public static List<String> getSalaryGrowRrate(List<String> salaryList) {
		LogManager
				.Method("Int Service : EmployeeRRUtilImpl -> getSalaryGrowRrate");
		float rate = 0;
		List<String> rateList = new ArrayList<>();
		rateList.add("0");

		DecimalFormat decimalFormat = new DecimalFormat(".00");
		float lastSumSalary = 0;
		float i = 0;
		for (String string : salaryList) {
			float sumSalary = Integer.parseInt(string);

			if (i == 0) {
				i++;
				lastSumSalary = sumSalary;
				continue;
			} else {
				if (lastSumSalary == 0) {
					rateList.add("0");
					lastSumSalary = sumSalary;
					continue;
				} else {
					rate = (sumSalary / lastSumSalary - 1) * 100;

					String rateStr = decimalFormat.format(rate);
					rateList.add(rateStr);
					i++;
					lastSumSalary = sumSalary;

				}

			}

		}
		LogManager
				.Method("Out Service : EmployeeRRUtilImpl -> getSalaryGrowRrate");
		return rateList;
	}

}
